﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_question_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlepType.DataSource = dal.ePioneer_QuestionType;
            ddlepType.DataTextField = "name";
            ddlepType.DataValueField = "id";
            ddlepType.DataBind();
            if (Request["id"] != null)
            {
                int id = int.Parse(Request["id"].ToString());
                HiddenFieldQid.Value = Request["id"];
                CMSModel.ePioneer_Question m_epc = dal.ePioneer_Question.FirstOrDefault(x => x.id == id);

                ddlepType.SelectedValue = m_epc.tid.ToString();
                txt_brief.Text = m_epc.Title;
                txt_content.Text = m_epc.Detail;
                CMSModel.ePioneer_Answer epa = dal.ePioneer_Answer.FirstOrDefault(p => p.question_id == id && p.IsChose == 1);
                if (epa != null)
                    txt_contentAnswer.Text = epa.Answer;
            }
        }
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        string userid = System.Configuration.ConfigurationManager.AppSettings["userid"].ToString();
        if (HiddenFieldQid.Value != "")
        {
            try
            {
                int id = int.Parse(HiddenFieldQid.Value);
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Question m_epc = dal.ePioneer_Question.FirstOrDefault(x => x.id == id);
                m_epc.Title = txt_brief.Text.Trim();
                m_epc.Detail = txt_content.Text.Trim();
                m_epc.tid = int.Parse(ddlepType.SelectedValue);
                m_epc.IsCheck = 1;
                m_epc.IsClose = 0;
                CMSModel.ePioneer_Answer epa = dal.ePioneer_Answer.FirstOrDefault(p => p.question_id == id && p.IsChose == 1);
                if (txt_contentAnswer.Text.Trim() != "")
                {
                    m_epc.IsClose = 1;
                    if (epa != null)
                    {
                        epa.IsCheck = 1;
                        epa.IsChose = 1;
                        epa.Answer = txt_contentAnswer.Text.Trim();
                    }
                    else
                    {
                        epa = new CMSModel.ePioneer_Answer();
                        epa.Answer = txt_contentAnswer.Text.Trim();
                        epa.A_Ok = 0;
                        epa.A_Good = 1;
                        epa.A_Bad = 0;
                        epa.CreateDate = DateTime.Now;
                        epa.IsCheck = 1;
                        epa.IsChose = 1;
                        epa.question_id = id;
                        epa.UserId = userid;
                        dal.AddToePioneer_Answer(epa);
                    }
                }
                else
                {

                    if (epa != null)
                    {
                        dal.DeleteObject(epa);
                    }
                }
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_question_check.aspx'", true);
            }
            catch (Exception ex)
            {
                //throw ex;
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败');", true);
            }
        }
        else
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.ePioneer_Question m_epc = new CMSModel.ePioneer_Question();
            m_epc.Title = txt_brief.Text.Trim();
            m_epc.Detail = txt_content.Text.Trim();
            m_epc.tid = int.Parse(ddlepType.SelectedValue);
            m_epc.Count = 0;
            m_epc.CreateDate = DateTime.Now;
            m_epc.IsCheck = 1;
            m_epc.IsClose = 0;
            m_epc.UserId = userid;
            try
            {
                if (txt_contentAnswer.Text.Trim() != "")
                {
                    m_epc.IsClose = 1;
                }
                dal.AddToePioneer_Question(m_epc);
                dal.SaveChanges();
                if (txt_contentAnswer.Text.Trim() != "")
                {
                    m_epc = dal.ePioneer_Question.Where(p => p.UserId == userid && p.CreateDate == m_epc.CreateDate).OrderByDescending(x => x.id).First();
                    CMSModel.ePioneer_Answer epa = new CMSModel.ePioneer_Answer();
                    epa.Answer = txt_contentAnswer.Text.Trim();
                    epa.A_Ok = 0;
                    epa.A_Good = 1;
                    epa.A_Bad = 0;
                    epa.CreateDate = DateTime.Now;
                    epa.IsCheck = 1;
                    epa.IsChose = 1;
                    epa.question_id = m_epc.id;
                    epa.UserId = userid;
                    dal.AddToePioneer_Answer(epa);
                    dal.SaveChanges();
                }
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='ep_question_check.aspx'", true);
            }
            catch (Exception)
            {

                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败');", true);
            }
        }
    }
}